#include "common_audio_path.h"

/* board include*/
#include "board_includes.h"
#include "common_includes.h"

/* private define ---------------------------------------------- */
#define IIS_RX_BUF_SIZE				(1024*4)
#define MIC_BUF_SIZE				(1024*2)

/* private type ---------------------------------------------- */
typedef struct
{
	uint32_t CurAudioPath;
	uint32_t PreAudioPath;
	bool esco_en;

	cbuffer_t record_i2s_cbuf_hdl;
	uint8_t *record_i2s_buffer;

	cbuffer_t mic2esco_cbuf_hdl;
	uint8_t *mic2esco_buffer;

}AudioPathVar_t;

/* prative variable---------------------------------------------- */
static AudioPathVar_t g_AudioPathVar;
static s16 aec_inbuf[3][256];
static u8 aec_index = 0;

/* private function prototypes ---------------------------------------------- */


/* prative function---------------------------------------------- */


/* Audio Input Nodo---------------------------------------------- */
void audio_input_bt_node(s16 *buf, u32 len)
{

}


static void audio_input_mic_node(s16 *buf, u32 len)
{
	if (common_audio_get_path() == AUDIO_PATH_MIC_TO_ESCO_TX)
	{
		cbuf_write(&g_AudioPathVar.mic2esco_cbuf_hdl, buf, len);
		//每次推送512个
		if (cbuf_get_data_size(&g_AudioPathVar.mic2esco_cbuf_hdl) >= 512)
		{
			cbuf_read(&g_AudioPathVar.mic2esco_cbuf_hdl, aec_inbuf[aec_index], 512);
			audio_aec_inbuf(aec_inbuf[aec_index], 512);
			if(++aec_index > 2){
				aec_index = 0;
			}
		}		
	}
}

static void audio_input_i2s_tx_node(s16 *buf, u32 len)
{
	
}

static void audio_input_i2s_rx_node(s16 *buf, u32 len)
{

}

/* Audio Ouput Nodo---------------------------------------------- */


/* public function---------------------------------------------- */

void common_audio_esco_flag_set(bool flag)
{
	g_AudioPathVar.esco_en = flag;
}

static bool common_audio_audio_path_create(uint32_t AudioPath)
{
	bool ret = FALSE;

	switch (AudioPath)
	{
		case AUDIO_PATH_MIC_TO_ESCO_TX:
		{
			g_AudioPathVar.mic2esco_buffer = (uint8_t *)malloc(MIC_BUF_SIZE);
			cbuf_init(&g_AudioPathVar.mic2esco_cbuf_hdl,g_AudioPathVar.mic2esco_buffer, MIC_BUF_SIZE);
			break;
		}

		default:break;
	}

	return TRUE;
}

static void common_audio_audio_path_destroy(uint32_t AudioPath)
{
	switch (AudioPath)
	{
		case AUDIO_PATH_MIC_TO_ESCO_TX:
		{
			free(g_AudioPathVar.mic2esco_buffer);
			break;
		}

		default:break;
	}

}


/* public variables -------------------------------------------------------- */
void common_audio_path_switch(uint32_t AudioPath)
{
	if (g_AudioPathVar.CurAudioPath == AudioPath)
	{
		return ;
	}
	
	g_AudioPathVar.PreAudioPath = g_AudioPathVar.CurAudioPath;
	common_audio_audio_path_destroy(g_AudioPathVar.PreAudioPath);
	
	common_audio_audio_path_create(AudioPath);
	g_AudioPathVar.CurAudioPath = AudioPath;
}

uint32_t common_audio_get_path(void)
{
	return g_AudioPathVar.CurAudioPath;
}

uint32_t common_audio_get_last_path(void)
{
	return g_AudioPathVar.PreAudioPath;
}

void common_audio_path_init(void)
{
	memset(&g_AudioPathVar, 0x00, sizeof(AudioPathVar_t));
	
	ej_bsp_iis_datas_cbfn_set(BSP_IIS_CH0, audio_input_i2s_tx_node, audio_input_i2s_rx_node);

	ej_bsp_mic_datas_cbfn_set(audio_input_mic_node);

	common_audio_path_switch(AUDIO_PATH_NONE);
}



